<?php

App::uses('AppController', 'Controller');

class IzvestajiController extends AppController{
    
    public $uses = array('Store', 'Production', 'Product');
    
    public $reports = array(
        'prodavnica' => 'Promet po prodavnici',
        'proizvod' => 'Promet po proizvodu'
    );
	
    public function beforeFilter() {
        $this->set("reports", $this->reports);
    }
    
	public function index(){
		
	}
	
	public function prodavnica() {
	    $filter = $this->data;
	    if(!empty($filter)) {
	        $filter = $filter['Filter'];
	        
	        if($filter['store_id']) {
	            if($store = $this->Store->findById($filter['store_id'])) {
	                $store = $store['Store'];
	            }
	        }
			
			if(!empty($store) && !empty($filter['date_start']) && !empty($filter['date_end'])) {
				if(! $data = $this->Production->find("all", array(
					"conditions" => array(
						'store_id' => $store['id'],
						'date >=' => $filter['date_start'], 
						'date <=' => $filter['date_end']
					),
				    'recursive' => 2
				))) {
					$message = 'Nema podataka za odabrani kriterijum.';
				}
			}else{
				$message = 'Morate odabrati sva polja!';
				$message_class = 'error';
			}
			
			$this->set(compact('filter', 'store', 'data', 'message', 'message_class'));
	    }
	}
	
	public function proizvod() {
	    $products = $this->Product->getOptions();
		
		$filter = @$this->data['Filter'];
        $data   = array();
        
        if (! empty($filter)) {
            if (!empty($filter['product_id']) && 
                !empty($filter['date_start']) && 
                !empty($filter['date_end'])) {
                
                $time_start = strtotime($filter['date_start']);
                $time_end   = strtotime($filter['date_end']);
                            
                if (! $data = $this->Production->reportByProduct($filter)) {
                    $message = 'Nema podataka za odabrani kriterijum.';
                }
            } else {
                $message = 'Morate odabrati Proizvod i Datume pretrage.';
            }
        }
		
		$this->set(compact('products', 'filter', 'store', 'data', 'message'));
	}
	
}